﻿using MemberManagement.Common;
using MemberManagement.Context;
using MemberManagement.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MemberManagement
{
    /// <summary>
    /// 新增编辑会员窗体
    /// </summary>
    public partial class CreateMemberForm : Form
    {
        /// <summary>
        /// 会员id
        /// </summary>
        public string MemberId { get; set; }
        private Member _member = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memberId">会员id</param>
        public CreateMemberForm()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtCardNumber.Text))
            {
                MessageBox.Show("卡号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtCardNumber.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtName.Focus();
                return;
            }
            
            ((Button)sender).Enabled = false;
            if (this.SaveMember())
            {
                string text = string.IsNullOrEmpty(this.MemberId) ? "发卡成功" : "更新成功";
                MessageBox.Show(text, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ((Button)sender).Enabled = true;
        }

        #region 私有函数
        private bool SaveMember()
        {
            try
            {
                using (var db = new MyContext())
                {
                    if (!string.IsNullOrEmpty(MemberId) && _member != null)
                    {
                        _member.IdCard = this.txtIdCard.Text.Trim();
                        _member.Name = this.txtName.Text.Trim();
                        _member.Mobile = this.txtMobile.Text.Trim();
                        _member.ModifyTime = DateTime.Now;
                        db.Update(_member);
                        db.SaveChanges();
                    }
                    else
                    {
                        if (db.Member.Count(m => m.CardNumber == this.txtCardNumber.Text.Trim()) > 0) throw new Exception("卡号已经存在");
                        var member = new Member()
                        {
                            CardNumber = this.txtCardNumber.Text.Trim(),
                            Name = this.txtName.Text.Trim(),
                            IdCard = this.txtIdCard.Text.Trim(),
                            Mobile = this.txtMobile.Text.Trim(),
                            Balance = this.txtBalance.Value,
                            ModifyTime = DateTime.Now
                        };
                        db.Member.Add(member);
                        //新增充值记录
                        db.Recharge.Add(new Recharge()
                        {
                            MemberId = member.Id,
                            Amount = this.txtBalance.Value,
                            Balance = this.txtBalance.Value,
                            Type =  Entity.Enum.RechargeType_Enum.IssueCard,
                            Remark = "发卡"
                        });
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private Member GetDetail(string memberId)
        {
            try
            {
                using (var db = new MyContext())
                {
                    var member = db.Member.FirstOrDefault(m => m.Id == memberId);
                    this._member = member;
                    if (member == null) throw new Exception("当前会员不存在");
                    this.txtIdCard.Text = member.IdCard;
                    this.txtName.Text = member.Name;
                    this.txtMobile.Text = member.Mobile;
                    this.txtCardNumber.Text = member.CardNumber;
                    this.txtBalance.Value = member.Balance;
                    return member;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        #endregion

        private void CreateMemberForm_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.MemberId))
            {
                this.txtCardNumber.Enabled = false;
                this.txtName.Enabled = false;
                this.txtBalance.Enabled = false;
                GetDetail(MemberId);
            }
            else
            {
                this.txtCardNumber.Text = DateTime.Now.ToString("yyyyMMddHHmmssff");
            }
        }
    }
}
